package com.lebang.controller.webapi;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Require;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.MemberRecordModel;
import com.lebang.model.back.Result;
import com.lebang.service.WebApiService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lebang.model.WebTask;
import com.lebang.model.enums.Status;
import com.lebang.service.*;
import com.lebang.service.buss.MemberRecordService;
import com.lebang.service.buss.WalletService;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author: ljx
 * @Date: 2020/9/4
 * @Version 1.0
 */
@RestController
@RequestMapping("/newweb")
public class NewWebApi {
    @Autowired
    private WalletService walletService;
    @Autowired
    private WebApiService webApiService;
    @Autowired
    private MemberRecordService memberRecordService;

    /**
     * 新web任务列表
     *
     * @param request
     * @param tagId
     * @param calssifyId
     * @return
     */
    @Require
    @PostMapping("/task/newlist")
    public JsonData newlist(HttpServletRequest request,
                            @RequestParam(value = "tagId", required = false) Integer tagId,
                            @RequestParam(value = "classifyId", required = false) Integer calssifyId) {
        if (TextUtil.isEmpty(tagId)){
            tagId=-1;
        }
        if (TextUtil.isEmpty(calssifyId)) {
            calssifyId = 0;
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        String brand = requestHeaders.getBrand();
        String appId = requestHeaders.getAppid();
        int userAllHistoryMoney = walletService.totalMoney(memberId, appId);
        List<WebTask> taskList = webApiService.getNewTaskList(memberId, brand, userAllHistoryMoney, Status.On.name(), calssifyId, tagId);
        return Callback.success("success", "查询成功", taskList);
    }

    /**
     * 任务提示
     *
     * @param request
     * @return
     */
    @Require
    @PostMapping("/task/remind")
    public JsonData taskRemind(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        //根据id查询待提交的任务
        Result<JSONObject> result = memberRecordService.getByMemberId(requestHeaders.getUserid());
        return Callback.success("success", "任务提示", result.getData());
    }

    @Require
    @PostMapping("/task/history")
    public JsonData history(HttpServletRequest request, Integer category, Long page, Integer limit) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        List<MemberRecordModel> records = webApiService.getTaskHistoryV2(requestHeaders.getUserid(), category, page, limit);
        return Callback.success("success", "查询成功", records);
    }


    @GetMapping("/share/{taskId}")
    public JsonData shareData(@PathVariable String taskId) {
        return Callback.success("success", "查询成功", webApiService.getShareModel(taskId));
    }

}
